import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store/index'
import * as mutationsTypes from '@/store/mutations-types'

Vue.use(VueRouter)

let routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    //登录
    path: '/home',
    name: 'home',
    component: () => import('@/views/home/Home'),
    beforeEnter: (to, from, next) => {
      console.log('从哪儿来: ' + from.path)
      console.log('到哪儿去: ' + to.path)
      window.document.title = '首页'
      store.commit(mutationsTypes.EXCHANGE_HOME_NAV_ACTIVE_INDEX, '1')
      next()
    }
  },
]

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


function getAllRouters(modules) {
  modules.keys().forEach((key) => {
    routes = routes.concat(modules(key).default);
  })
}

getAllRouters(require.context('@/routes', true, /\.routes\.js$/))


const router = new VueRouter({
  routes,
  mode: 'history',
  meta: {
    title: '',
  }
})

export default router
